package com.qiqitrue.mojo;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * <p>
 * 参数插件测试
 * </p>
 *
 * @author: laiqi
 * @since: 2021-12-02
 */
@Mojo(name = "parameter")
public class ParameterMojo extends AbstractMojo {

    /**
     * 要显示的问候语。
     */
    @Parameter(property = "sayhi.greeting", defaultValue = "Hello World!")
    private String greeting;

    @Parameter
    private boolean myBoolean;

    @Parameter
    private Integer myInteger;

    @Parameter
    private File myFile;

    public enum Color {
        GREEN,
        RED,
        BLUE
    }

    @Parameter
    private Color myColor;

    @Parameter
    private String[] myArray;

    @Parameter
    private List myList;

    @Parameter
    private Map myMap;

    @Parameter
    private Properties myProperties;

    public static class Person {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    @Parameter
    private Person person;


    @Override
    public void execute() {
        Field[] declaredFields = ParameterMojo.class.getDeclaredFields();
        Arrays.stream(declaredFields).forEach(field -> {
            if (field.isAccessible()) {
                field.setAccessible(true);
            }

            try {
                // 输出打印类所有属性
                this.getLog().info(field.getName() + ":" + field.get(this));
            } catch (IllegalAccessException e) {
                this.getLog().warn(e);
            }
        });
    }
}